//
//  SomePastiApp.swift
//  SomePasti
//
//  Created by 李永彬 on 2025/10/4.
//

import SwiftUI
import IQKeyboardManagerSwift
import IQKeyboardToolbarManager

@main
struct SomePastiApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @StateObject private var appState = SMAppState.shared
    @StateObject private var loginInfo = SMLoginInfo.shared
    
    init() {
        SMProgressHUD.configure()
        configureGlobalNavigationBar() // 全局配置导航栏
        
        let manager = IQKeyboardManager.shared
        manager.isEnabled = true
        manager.layoutIfNeededOnUpdate = true
        manager.resignOnTouchOutside = true
        IQKeyboardToolbarManager.shared.isEnabled = false
        
        updateRootView()
    }
    
    var body: some Scene {
        WindowGroup {
            Group {
                switch appState.rootView {
                case .login:
                    SMLoginView()
                case .main:
                    SMTabBarView()
                }
            }
            .fullScreenCover(isPresented: $loginInfo.shouldPresentLogInView) {
                SMLoginView()
            }
            .environmentObject(appState)
        }
    }
    
    // 全局导航栏配置
    private func configureGlobalNavigationBar() {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        
        // 1. 设置导航栏背景色
        appearance.backgroundColor = UIColor(.white) // 你的全局背景色
        
        // 2. 设置标题样式
        appearance.titleTextAttributes = [
            .font: UIFont.systemFont(ofSize: 18, weight: .semibold),
            .foregroundColor: UIColor(.black) // 你的全局标题颜色
        ]
        
        // 3. 设置返回按钮样式
        let backImage = UIImage(systemName: "chevron.left")?
            .withTintColor(.black, renderingMode: .alwaysOriginal) // 你的全局返回按钮颜色
        appearance.setBackIndicatorImage(backImage, transitionMaskImage: backImage)
        
        // 4. 隐藏返回按钮文字
        appearance.backButtonAppearance.normal.titleTextAttributes = [
            .foregroundColor: UIColor.clear
        ]
        
        // 5. 移除导航栏底部分隔线
        appearance.shadowColor = .clear
        appearance.shadowImage = UIImage()
        
        // 6. 应用到所有导航栏样式
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
        UINavigationBar.appearance().compactAppearance = appearance
        if #available(iOS 15.0, *) {
            UINavigationBar.appearance().compactScrollEdgeAppearance = appearance
        } else {
            // Fallback on earlier versions
        }
        
        // 7. 设置返回按钮颜色
        UINavigationBar.appearance().tintColor = Color(hex: "#000000").toUIColor()
    }
    
    private func updateRootView() {
        if SMLoginInfo.shared.isLogined {
            SMAppState.shared.rootView = .main
        } else {
            SMAppState.shared.rootView = .login
        }
    }
}

class SMAppState: ObservableObject {
    static let shared = SMAppState()
    @Published var rootView: RootViewType = .login
    enum RootViewType {
        case login, main
    }
}
